﻿using Summer.Network;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Proto;
using Summer;
using GameServer.Model;
using GameServer.Mgr;
using static Summer.Network.MessageRouter;

namespace GameServer.Service
{
    /// <summary>
    /// 地图服务
    /// </summary>
    public class SpaceService : Singleton<SpaceService>
    {
        

        public void Start()
        {

            //初始化地图
            SpaceManager.Instance.Init();

            //位置同步请求
            MessageRouter.Instance.Subscribe<SpaceEntitySyncRequest>(_SpaceEntitySyncRequest);

            

        }
        

        public Space GetSpace(int spaceId)
        {
            return SpaceManager.Instance.GetSpace(spaceId);
        }


        private void _SpaceEntitySyncRequest(Connection conn, SpaceEntitySyncRequest msg)
        {
            //获取当前角色所在的地图
            var space = conn.Get<Character>()?.Space;
            if (space == null) return;
            space.UpdateEntity(msg.EntitySync);
        }
    }
}
